<?php

declare(strict_types=1);

namespace app\model;

use think\Model;
use think\facade\Db;

/**
 * @mixin \think\Model
 */
class Menus extends Model
{
    //获取菜单单条数据
    public static function find($id)
    {
        $res = Db::name('menus')->where('id', $id)->where('del', '0')->find();
        return $res;
    }
    //获取菜单列表
    public static function list($where = '')
    {
        $res = Db::name('menus')->where($where)->where('del', '0')->order('px','desc')->select();
        return $res;
    }

    //菜单添加
    public static function add($data)
    {
        $data['add_date'] = time();
        $res = Db::name('menus')->insert($data);
        return $res;
    }

    //菜单编辑
    public static function edit($data)
    {
        $data['rep_date'] = time();
        $res = Db::name('menus')->update($data);
        return $res;
    }

    //菜单删除
    public static function del($id)
    {
        $son = Db::name('menus')->where('parent_id', $id)->where('del', '0')->find();
        if ($son) {
            return '删除失败，请先删除下级';
        }
        $fild = Db::name('menus')->where('id', $id)->where('del', '0')->find();
        if ($fild['controller'] == "MenusItem" && $fild['action'] == "list") {
            return '菜单功能不能删除！';
        }
        if ($fild) {
            $res = Db::name('menus')->where('id', $id)->update(['rep_date' => time(), 'del' => '1']);
            if ($res) {
                return 'success';
            } else {
                return '删除失败!';
            }
        } else {
            return '参数错误';
        }
    }
}
